//
//  oldSettingsViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/4/10.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldSettingsViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "oldLoginViewController.h"
#import "AppDelegate.h"

@interface oldSettingsViewController ()

@end

@implementation oldSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    self.navigationItem.title = @"设置";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}

#pragma mark - initData
- (void)initData
{
    
}

#pragma mark 清理缓存图片

//清除缓存图片
- (void)clearTmpPics
{
    
    [[SDImageCache sharedImageCache] clearDisk];
    
    [[SDImageCache sharedImageCache] clearMemory];//可有可无
    
    //    [[ZcSQLite sharedInstance] clearAllTable:db];//清除数据库表中的数据
    [self.tableView reloadData];
    [oldHub warningUser:@"缓存清除成功"];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.textColor = OLD_BLACK;
    cell.detailTextLabel.textColor = OLD_BLACK;
    
    switch (indexPath.row) {
        case 0:
        {
            cell.textLabel.text = @"清除缓存";
            float totalSize = [[SDImageCache sharedImageCache] getSize];
            totalSize = totalSize/1024.0/1024.0;
            
            NSString *clearCacheName = totalSize >= 1 ? [NSString stringWithFormat:@"已用缓存(%.2fM)",totalSize] : [NSString stringWithFormat:@"已用缓存(%.2fK)",totalSize * 1024];
            cell.detailTextLabel.text = clearCacheName;
        }
            break;
        case 1:
        {
            cell.textLabel.text = @"退出登录";
        }
            break;
        default:
            break;
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        [self clearTmpPics];
    }
    else if (indexPath.row == 1) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"退出登录" message:@"确定要退出登录吗？" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *chooseReason = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self loginout];
        }];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:chooseReason];
        [alert addAction:cancel];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
}

- (void)loginout
{
    [ACCOUNT_SINGLETON clearAccountInfo];
    
    oldLoginViewController *loginVC= [[oldLoginViewController alloc] initWithNibName:@"oldLoginViewController" bundle:nil];
    MyNavigationController *navi = [[MyNavigationController alloc] initWithRootViewController:loginVC];
    UIApplication *app = [UIApplication sharedApplication];
    app.delegate.window.rootViewController = navi;
    [app.delegate.window makeKeyAndVisible];
}


@end
